package org.microdream.easy.deploy.service;

import java.util.Map;

import javax.annotation.Resource;

import org.microdream.easy.deploy.dao.DServerAuthRepoitory;
import org.microdream.easy.deploy.po.DServerAuth;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import ice.server.comm.BaseServiceImpl;
import ice.server.comm.CommRepository;
import ice.server.comm.ParamBulider;
import ice.tool.ResultMap;

/**
 * 服务器账号信息服务层
 * @author zhangrenbin
 *
 */
@Service
public class DServerAuthService extends BaseServiceImpl<DServerAuth, Integer> {
	
	@Resource
	private DServerAuthRepoitory dServerAuthRepoitory;

	@Override
	public CommRepository<DServerAuth, Integer> getRepository() {
		return dServerAuthRepoitory;
	}

	@Override
	public Integer getIdenty(DServerAuth t) {
		return t.getId();
	}

	public Map<String, Object> addAuth(DServerAuth auth) {
		long cnt = this.cnt(ParamBulider.build().addEqual("serverCode", auth.getServerCode()).
				addEqual("username", auth.getUsername()).get());
		if(cnt > 0) {
			return ResultMap.build().add(false).message("此用户已经存在!").get();
		}
		this.save(auth);
		return ResultMap.build().add(true).get();
	}
	
	public Map<String, Object> modAuth(DServerAuth auth) {
		DServerAuth a = this.get(auth.getId());
		a.setUserpwd(auth.getUserpwd());
		this.save(a);
		return ResultMap.build().add(true).get();
	}
	
	public Map<String, Object> delAuth(@PathVariable int id) {
		// 判断此用户是否被使用，被使用则无法删除
		if(isUsed(id)) {
			return ResultMap.build().delete(false).message("此用户已经被使用，无法进行删除").get();
		}
		this.delete(id);
		return ResultMap.build().delete(true).get();
	}
	
	private boolean isUsed(int id) {
		// TODO: 判断
		return false;
	}
}
